import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import os
import re
import warnings
import matplotlib.pyplot as plt
import seaborn as sns
import librosa
import librosa.display
from sklearn.preprocessing import minmax_scale
import IPython.display as ipd
plt.rcParams['figure.figsize'] = (20,8)
plt.rcParams['font.size'] = 16
sns.set_style('darkgrid')
warnings.filterwarnings("ignore")
directory = 'input/dysarthria-detection'
data = pd.read_csv("input/dysarthria-detection/torgo_data/data.csv")
data['filename'] = data['filename'].apply(lambda x: os.path.join(directory,x))
data
| is_dysarthria | gender | filename | |
|---|---|---|---|
| 0 | non_dysarthria | female | input/dysarthria-detection/torgo_data/non_dysa... |
| 1 | non_dysarthria | female | input/dysarthria-detection/torgo_data/non_dysa... |
| 2 | non_dysarthria | female | input/dysarthria-detection/torgo_data/non_dysa... |
| 3 | non_dysarthria | female | input/dysarthria-detection/torgo_data/non_dysa... |
| 4 | non_dysarthria | female | input/dysarthria-detection/torgo_data/non_dysa... |
| ... | ... | ... | ... |
| 1995 | dysarthria | male | input/dysarthria-detection/torgo_data/dysarthr... |
| 1996 | dysarthria | male | input/dysarthria-detection/torgo_data/dysarthr... |
| 1997 | dysarthria | male | input/dysarthria-detection/torgo_data/dysarthr... |
| 1998 | dysarthria | male | input/dysarthria-detection/torgo_data/dysarthr... |
| 1999 | dysarthria | male | input/dysarthria-detection/torgo_data/dysarthr... |
2000 rows × 3 columns
Spectrogram, Waveplot, Zero Crossing Rate, Spectral Rolloff, MFCCs, Mel Spectrogram, and Spectral Centroids
def show_waveplot(audio_path,label,gender):
x , sr = librosa.load(audio_path)
plt.figure(figsize=(20, 6))
librosa.display.waveshow(x, sr=sr)
plt.title(f"Waveplot: of Class: {label}, Gender: {gender}")
def show_spectrogram(audio_path,label,gender):
x , sr = librosa.load(audio_path)
X = librosa.stft(x)
Xdb = librosa.amplitude_to_db(abs(X))
plt.figure(figsize=(20,6))
librosa.display.specshow(Xdb, sr=sr, x_axis='time', y_axis='hz',cmap='plasma')
plt.colorbar()
plt.title(f"Spectrogram of Class: {label}, Gender: {gender}")
def show_zcr(audio_path,label,gender):
x , sr = librosa.load(audio_path)
zero_crossings = librosa.zero_crossings(x)
print("Sum of zero crossing ", zero_crossings.sum())
plt.figure(figsize=(20, 5))
plt.title(f'Zero Crossing Rate of Class: {label}, Gender: {gender}')
zcrs = librosa.feature.zero_crossing_rate(x)
plt.plot(zcrs[0])
plt.show()
def normalize(x, axis=0):
return minmax_scale(x, axis=axis)
def show_spectral_centroids(audio_path,label,gender):
plt.figure(figsize=(20, 5))
plt.title(f'Spectral Centroids of Class: {label}, Gender: {gender}')
x , sr = librosa.load(audio_path)
spectral_centroids = librosa.feature.spectral_centroid(x, sr=sr)[0]
frames = range(len(spectral_centroids))
t = librosa.frames_to_time(frames)
librosa.display.waveshow(x, sr=sr, alpha=0.4)
plt.plot(t, normalize(spectral_centroids), color='r')
plt.show()
def show_spectral_rolloff(audio_path,label,gender):
plt.figure(figsize=(20, 5))
plt.title(f'Spectral Rolloff of Class: {label}, Gender: {gender}')
x , sr = librosa.load(audio_path)
spectral_rolloff = librosa.feature.spectral_rolloff(x, sr=sr, roll_percent=0.01)[0]
frames = range(len(spectral_rolloff))
t = librosa.frames_to_time(frames)
librosa.display.waveshow(x, sr=sr, alpha=0.4)
plt.plot(t, normalize(spectral_rolloff), color='r')
plt.show()
def show_mfccs(audio_path,label,gender):
plt.figure(figsize=(20, 6))
plt.title(f'MFCC of Class: {label}, Gender: {gender}')
x , sr = librosa.load(audio_path)
mfccs = librosa.feature.mfcc(y=x, sr=sr)
librosa.display.specshow(mfccs, sr=sr, x_axis='time',cmap='plasma')
plt.show()
def show_melspectro(audio_path,label,gender):
plt.figure(figsize=(20, 6))
plt.title(f'Mel Spectro of Class: {label}, Gender: {gender}')
x , sr = librosa.load(audio_path)
melspectro = librosa.feature.melspectrogram(y=x, sr=sr)
librosa.display.specshow(melspectro, sr=sr, x_axis='time',cmap='plasma')
plt.show()
data.sample(frac=1).reset_index(drop=True,inplace=True)
male_dysarthric = data[(data['gender']=='male') & (data['is_dysarthria']=='dysarthria')].sample(1)
female_dysarthric = data[(data['gender']=='female') & (data['is_dysarthria']=='dysarthria')].sample(1)
male_ndysarthric = data[(data['gender']=='male') & (data['is_dysarthria']=='non_dysarthria')].sample(1)
female_ndysarthric = data[(data['gender']=='female') & (data['is_dysarthria']=='non_dysarthria')].sample(1)
ipd.Audio(male_dysarthric.iloc[0].filename)
ipd.Audio(female_dysarthric.iloc[0].filename)
ipd.Audio(male_ndysarthric.iloc[0].filename)
ipd.Audio(female_ndysarthric.iloc[0].filename)
show_waveplot(male_dysarthric.iloc[0].filename, male_dysarthric.iloc[0].is_dysarthria, male_dysarthric.iloc[0].gender)
show_waveplot(female_dysarthric.iloc[0].filename, female_dysarthric.iloc[0].is_dysarthria, female_dysarthric.iloc[0].gender)
show_waveplot(male_ndysarthric.iloc[0].filename, male_ndysarthric.iloc[0].is_dysarthria, male_ndysarthric.iloc[0].gender)
show_waveplot(female_ndysarthric.iloc[0].filename, female_ndysarthric.iloc[0].is_dysarthria, female_ndysarthric.iloc[0].gender)
show_spectrogram(male_dysarthric.iloc[0].filename, male_dysarthric.iloc[0].is_dysarthria, male_dysarthric.iloc[0].gender)
show_spectrogram(female_dysarthric.iloc[0].filename, female_dysarthric.iloc[0].is_dysarthria, female_dysarthric.iloc[0].gender)
show_spectrogram(male_ndysarthric.iloc[0].filename, male_ndysarthric.iloc[0].is_dysarthria, male_ndysarthric.iloc[0].gender)
show_spectrogram(female_ndysarthric.iloc[0].filename, female_ndysarthric.iloc[0].is_dysarthria, female_ndysarthric.iloc[0].gender)
show_zcr(male_dysarthric.iloc[0].filename, male_dysarthric.iloc[0].is_dysarthria, male_dysarthric.iloc[0].gender)
show_zcr(female_dysarthric.iloc[0].filename, female_dysarthric.iloc[0].is_dysarthria, female_dysarthric.iloc[0].gender)
show_zcr(male_ndysarthric.iloc[0].filename, male_ndysarthric.iloc[0].is_dysarthria, male_ndysarthric.iloc[0].gender)
show_zcr(female_ndysarthric.iloc[0].filename, female_ndysarthric.iloc[0].is_dysarthria, female_ndysarthric.iloc[0].gender)
Sum of zero crossing 5804
Sum of zero crossing 5512
Sum of zero crossing 1992
Sum of zero crossing 4240
show_spectral_centroids(male_dysarthric.iloc[0].filename, male_dysarthric.iloc[0].is_dysarthria, male_dysarthric.iloc[0].gender)
show_spectral_centroids(female_dysarthric.iloc[0].filename, female_dysarthric.iloc[0].is_dysarthria, female_dysarthric.iloc[0].gender)
show_spectral_centroids(male_ndysarthric.iloc[0].filename, male_ndysarthric.iloc[0].is_dysarthria, male_ndysarthric.iloc[0].gender)
show_spectral_centroids(female_ndysarthric.iloc[0].filename, female_ndysarthric.iloc[0].is_dysarthria, female_ndysarthric.iloc[0].gender)
show_spectral_rolloff(male_dysarthric.iloc[0].filename, male_dysarthric.iloc[0].is_dysarthria, male_dysarthric.iloc[0].gender)
show_spectral_rolloff(female_dysarthric.iloc[0].filename, female_dysarthric.iloc[0].is_dysarthria, female_dysarthric.iloc[0].gender)
show_spectral_rolloff(male_ndysarthric.iloc[0].filename, male_ndysarthric.iloc[0].is_dysarthria, male_ndysarthric.iloc[0].gender)
show_spectral_rolloff(female_ndysarthric.iloc[0].filename, female_ndysarthric.iloc[0].is_dysarthria, female_ndysarthric.iloc[0].gender)